package ch.trackedbean.client.gui;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import ch.trackedbean.client.*;
import ch.trackedbean.tracking.injection.*;

/**
 * The real main class but loded using {@link TrackedBeanInjection}.
 * 
 * @author hautle
 */
public class MainFrame extends JFrame {
    /** Singleton reference. */
    public static final MainFrame ME = new MainFrame();

    /** The desktop pane. */
    private JDesktopPane desk = new JDesktopPane();

    /** The toppanel holding contributed actions. */
    private JPanel actions = new JPanel();

    /** The statusbar at the window bottom. */
    private StatusBar statusBar = new StatusBar();

    /** Mapping of panels an the internal frame used to display it. */
    private IdentityHashMap<JPanel, JInternalFrame> frames = new IdentityHashMap<JPanel, JInternalFrame>();

    /**
     * Default constructor.
     */
    private MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Displays the given panel inside a {@link JInternalFrame}.
     * 
     * @param title The frame title
     * @param content The content of the frame
     * @return The created frame
     */
    public JInternalFrame showWindow(String title, JPanel content) {
        JInternalFrame frame = new JInternalFrame(title, true, true, true);
        frame.setLayout(new BorderLayout());
        frame.add(content, BorderLayout.CENTER);
        desk.add(frame);
        frame.pack();
        frame.setVisible(true);
        frames.put(content, frame);
        return frame;
    }

    /**
     * Disposes the {@link JInternalFrame} displaying the given panel.
     * 
     * @param content The content panel to 'drop'
     */
    public void disposeWindow(JPanel content) {
        final JInternalFrame frame = frames.remove(content);
        if (frame == null)
            return;
        frame.dispose();
    }

    /**
     * Adds a button for the given action in the top panel.
     * 
     * @param a The action
     */
    public void addAction(Action a) {
        actions.add(new JButton(a));
    }

    /**
     * Displays the progressbar with the given text.
     * 
     * @param text The text
     */
    public void showProgressBar(String text) {
        statusBar.showWork(text);
    }

    /**
     * Hiddes the progressbar.
     */
    public void hiddeProgressBar() {
        statusBar.hidde();
    }

    /**
     * Displays the window.
     */
    private void setUp() {
        setLayout(new BorderLayout());
        add(actions, BorderLayout.NORTH);
        add(desk, BorderLayout.CENTER);
        add(statusBar, BorderLayout.SOUTH);
        setSize(400, 400);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MainFrame.ME.setUp();
                new APlugin().init();
                MainFrame.ME.setVisible(true);
            }
        });
    }

    /**
     * The statusbar.
     * 
     * @author hautle
     */
    private static class StatusBar extends JPanel {
        /** The text of the status bar. */
        private JLabel text = new JLabel();

        /** The progressbar. */
        private JProgressBar progress = new JProgressBar();

        /**
         * Default constructor.
         */
        public StatusBar() {
            super(new FlowLayout(FlowLayout.LEFT));
            add(text);
            add(Box.createHorizontalStrut(5));
            add(progress);
            hidde();
        }

        /**
         * Shows the progressbar.
         * 
         * @param text The status text
         */
        void showWork(String text) {
            this.text.setText(text);
            this.progress.setIndeterminate(true);
            this.text.setVisible(true);
            this.progress.setVisible(true);
        }

        /**
         * Hiddes the progressbar.
         */
        void hidde() {
            text.setVisible(false);
            progress.setVisible(false);
            text.setText("");
            progress.setValue(0);
        }

    }
}
